<?php
/**
 * Created by PhpStorm.
 * Name：官网首页
 * User: 董坤鸿
 * Date: 2016/11/14
 * Time: 10:04
 */

namespace App\Http\Controllers;
use App\Services\WxClientService;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    /**
     * 官网首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('welcome');
    }

    /**
     * 二维码下载
     */
    public function qrCode(){
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
            //IOS下载
            $iosUrl = config('download.ios_download_name');
            return redirect($iosUrl);
        } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Android')) {
            //安卓下载
            return redirect('downloads');
        } else {
            return redirect('downloads');
        }
    }

    /**
     * 本地下载
     */
    public function downloads()
    {
        //下载名称
        $fileName = config("download.android_download_name");
        if (!preg_match('/^\//', $fileName)) {
            $fileName = storage_path($fileName);
        }

        //判断下载包是否存在
        if (!file_exists($fileName)) abort("404");

        //设置类型
        header("Content-Type: application/octet-stream");
        //设置附件处理，浏览器形成下载或打开
        header("Content-Disposition: attachment;filename=" . $fileName);
        //设置大小
        header("Content-Length: " . filesize($fileName));
        //加载要下载的文件
        readfile($fileName);
    }

    /**
     * 根据传入的微信号ID，返回该微信号所属渠道使用的插件名称
     * @param $wxClientId
     * @param WxClientService $service
     * @return \Illuminate\Http\JsonResponse
     */
    public function pluginName($wxClientId, WxClientService $service)
    {
        $pluginName = $service->getPluginNameById($wxClientId);
        return response()->json(compact('pluginName'));
    }

    /**
     * 检查是否允许添加好友
     * @param WxClientService $service
     * @return array
     */
    public function checkVerifyMsg(WxClientService $service)
    {
        $uin = request()->input('uin', 0);
        $account = request()->input('account', '');
        $success = false;
        if ($uin && is_numeric($uin) && $account) {
            $success = $service->checkVerifyMsg(intval($uin), $account);
        }
        return compact('success');
    }
}